package com.intpro.demo.Utils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import org.springframework.util.StringUtils;

public class JsonUtil
{


	private static ObjectMapper objectMapper = new ObjectMapper();

	/**
	 * 对象转换成json
	 */
	public static <T> String objectToJson(T obj)
	{
		if (obj == null)
		{
			return null;
		}
		try
		{
			return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 对象转换成格式化的json
	 */
	public static <T> String objectToJsonPretty(T obj)
	{
		if (obj == null)
		{
			return null;
		}
		try
		{
			return obj instanceof String ? (String) obj
					: objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 将json转换成对象Class
	 */
	public static <T> T jsonToObject(String src, Class<T> clazz)
	{
		if (StringUtils.isEmpty(src) || clazz == null)
		{
			return null;
		}
		try
		{
			return clazz.equals(String.class) ? (T) src : objectMapper.readValue(src, clazz);
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 将json转换成对象TypeReference
	 */
	public static <T> T jsonToObject(String src, TypeReference<T> typeReference)
	{
		if (StringUtils.isEmpty(src) || typeReference == null)
		{
			return null;
		}
		try
		{
			return (T) (typeReference.getType().equals(String.class) ? src
					: objectMapper.readValue(src, typeReference));
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 将json转换成对象
	 */
	public static <T> T jsonToObject(String src, Class<?> collectionClass,
			Class<?>... elementClasses)
	{
		JavaType javaType = objectMapper.getTypeFactory()
				.constructParametricType(collectionClass, elementClasses);
		try
		{
			return objectMapper.readValue(src, javaType);
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

}
